<script setup>

import { ref } from "vue";
import { useRouter } from "vue-router";

import tabbarData from "@/assets/data/tabbar.js";
import { getImageUrl } from "@/utils/load_assets.js";

const router = useRouter();

const currentIndex = ref(0);
const itemClick = (item, index) => {
  currentIndex.value = index;
  router.push(item.path);
};

</script>

<template>
  <div class="tabbar">
    <template v-for="(item, index) in tabbarData" :key="index">
      <div class="tabbar-item" @click="itemClick(item, index)" :class="{ active: currentIndex === index }">
        <img v-if="currentIndex === index" :src="getImageUrl(item.imageActive)" />
        <img v-else :src="getImageUrl(item.image)" />
        <span class="text">{{ item.text }}</span>
      </div>
    </template>
  </div>
</template>

<style lang="less" scoped>
.tabbar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 50px;
  display: flex;
  border: 1px solid #f5f5f5;

  .tabbar-item {
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;

    // &表示其所在选择器的名称
    &.active {
      color: var(--primary-color);
    }

    img {
      width: 36px;
    }

    .text {
      margin-top: 2px;
      font-size: 12px;
    }
  }
}
</style>
